﻿using System;
using System.Text;
using BO.Infrastructure.Utils;
using BugManager;
using Utilities.Entities;

namespace GUI.Controls.Embed
{
    public partial class EmbedTemplate5 : System.Web.UI.UserControl
    {
        private const string RelateNewsFormat = "<li><a href=\"{0}\" title=\"{2}\">{1}</a></li>";

        private const string LiFormat =
            "<li>{0}<div class=\"scope clearfix\"><h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>{3}</div></li>";

        private const int ImageWidth = 240;
        private const int ImageHeight = 180;

        private const int ImageWidth2 = 150;
        private const int ImageHeight2 = 100;

        private const string LinkFormat = "<a href=\"{0}\" title=\"{2}\">{1}</a>";

        public bool DataBind(CategoryObject category, NewsObject[] data, NewsObject[] otherNews, bool displayRelateNews = true)
        {
            try
            {
                if (category != null)
                {
                    ltrBoxTitle.Text = string.Format(LinkFormat, category.Url, category.Name,
                                                     PresentationUtils.EnCode(category.Name));
                }

                if (data != null && data.Length > 0)
                {
                    var sb = new StringBuilder();
                    foreach (var news in data)
                    {
                        var relateNews = string.Empty;
                        if (displayRelateNews && news.RelateNews != null && news.RelateNews.Length > 0)
                            relateNews = BuildRelateNews(news.RelateNews);
                        var avatar = PresentationUtils.Photo.BuildPhoto(news.Avatar, news.Title, news.Url, ImageWidth,
                                                                        ImageHeight, lazyLoad: true);
                        sb.AppendFormat(
                            "<li>{3}<div class=\"scope clearfix\"><h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>{4}</div></li>",
                            news.Url,
                            news.Title,
                            PresentationUtils.EnCode(news.Title),
                            avatar,
                            relateNews);
                    }
                    ltrData.Text = sb.ToString();
                }

                if (otherNews != null && otherNews.Length > 0)
                {
                    var sb = new StringBuilder();
                    foreach (var news in otherNews)
                    {
                        var avatar = PresentationUtils.Photo.BuildPhoto(news.Avatar, news.Title, news.Url, ImageWidth2, ImageHeight2, lazyLoad: true);
                        sb.AppendFormat("<li>{3}<div class=\"scope clearfix\"><h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2></div></li>",
                            news.Url,
                            news.Title,
                            PresentationUtils.EnCode(news.Title),
                            avatar);
                    }
                    ltrOtherNews.Text = string.Format("<ul class=\"list list-other clearfix\">{0}</ul>", sb);
                }

                return true;
            }
            catch (Exception ex)
            {
                ErrorManager.Log(ex.ToString());
                return false;
            }
        }

        public static string BuildRelateNews(NewsObject[] data)
        {
            if (data != null && data.Length > 0)
            {
                var sb = new StringBuilder();
                foreach (var news in data)
                {
                    sb.AppendFormat(
                        "<li><a href=\"{0}\" title=\"{2}\"><span class=\"liststyle3 sprite fl\"></span>{1}</a></li>",
                        news.Url,
                        news.Title,
                        PresentationUtils.EnCode(news.Title));
                }
                return string.Format("<ul class=\"relation_news fr\">{0}</ul>", sb);
            }
            return string.Empty;
        }
    }
}